﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ttbit.core;
using ttbit.math;

namespace ttbit.core
{
    public class SpriteData
    {
        public bool IsGray;
        public ushort Width;
        public ushort Height;
        public byte[] Data;
    }
    public class Sprite
    {
        //public string name;
        public Texture root;
        //uv
        public ushort umin;
        public ushort umax;
        public ushort vmin;
        public ushort vmax;
        public ushort uvwidth
        {
            get
            {
                return (ushort)(umax - umin);
            }
        }
        public ushort uvheight
        {
            get
            {
                return (ushort)(vmax - vmin);
            }
        }
        //尺寸 uv可能小于尺寸
        public ushort Width;//sprite的宽度
        public ushort Height;//sprite的高度

        //留白
        public byte borderX;//sprite 的留白
        public byte borderY;//sprite的留白
        public byte borderX2
        {
            get
            {
                return (byte)(Width - (umax - umin));
            }
        }
        public byte borderY2
        {
            get
            {
                return (byte)(Height - (vmax - vmin));
            }
        }

        public static void FillRect(Sprite sprite, RenderVertex[] vbuf, int vbufpos, int[] ibuf, int ibufpos, float scaleX, float scaleY, float offsetX, float offsetY, Color color, bool keepx = true, bool keepy = true)
        {
            RenderVertex v0;
            RenderVertex v1;
            RenderVertex v2;
            RenderVertex v3;
            int texSizeX = sprite.root.Width;
            int texSizeY = sprite.root.Height;
            float usize = sprite.umax - sprite.umin;
            float vsize = sprite.vmax - sprite.vmin;

            //scaleX =scaleX* sprite.Width / usize;
            //scaleY =scaleY* sprite.Height / vsize;

            float ux1 = (float)sprite.umin / texSizeX;
            float ux2 = (float)sprite.umax / texSizeX;
            float uy1 = (float)sprite.vmin / texSizeY;
            float uy2 = (float)sprite.vmax / texSizeY;

            float x1 = keepx ? sprite.borderX * scaleX + offsetX : offsetX;
            float x2 = (float)x1 + (float)usize * scaleX;
            float y1 = keepy ? sprite.borderY * scaleY + offsetY : offsetY;
            float y2 = (float)y1 + (float)vsize * scaleY;

            v0.X = x1;
            v0.Y = y1;
            v0.Z = 0;
            v0.U = ux1;
            v0.V = uy1;
            v0.R = color.R;
            v0.G = color.G;
            v0.B = color.B;
            v0.A = color.A;
            v0.U2 = 0;
            v0.V2 = 0;
            v0.UPal = 0;
            v0.VPal = 0;
            v1.X = x2;
            v1.Y = y1;
            v1.Z = 0;
            v1.U = ux2;
            v1.V = uy1;
            v1.R = color.R;
            v1.G = color.G;
            v1.B = color.B;
            v1.A = color.A;
            v1.U2 = 0;
            v1.V2 = 0;
            v1.UPal = 0;
            v1.VPal = 0;
            v2.X = x1;
            v2.Y = y2;
            v2.Z = 0;
            v2.U = ux1;
            v2.V = uy2;
            v2.R = color.R;
            v2.G = color.G;
            v2.B = color.B;
            v2.A = color.A;
            v2.U2 = 0;
            v2.V2 = 0;
            v2.UPal = 0;
            v2.VPal = 0;
            v3.X = x2;
            v3.Y = y2;
            v3.Z = 0;
            v3.U = ux2;
            v3.V = uy2;
            v3.R = color.R;
            v3.G = color.G;
            v3.B = color.B;
            v3.A = color.A;
            v3.U2 = 0;
            v3.V2 = 0;
            v3.UPal = 0;
            v3.VPal = 0;
            vbuf[vbufpos + 0] = v0;
            vbuf[vbufpos + 1] = v1;
            vbuf[vbufpos + 2] = v2;
            vbuf[vbufpos + 3] = v3;
            if (ibuf != null)
            {
                ibuf[ibufpos + 0] = vbufpos + 0;
                ibuf[ibufpos + 1] = vbufpos + 1;
                ibuf[ibufpos + 2] = vbufpos + 2;
                ibuf[ibufpos + 3] = vbufpos + 2;
                ibuf[ibufpos + 4] = vbufpos + 1;
                ibuf[ibufpos + 5] = vbufpos + 3;
            }
        }

        public static void FillRectEmpty(RenderVertex[] vbuf, int vbufpos, int[] ibuf, int ibufpos)
        {
            if (ibuf != null)
            {
                ibuf[ibufpos + 0] = vbufpos + 0;
                ibuf[ibufpos + 1] = vbufpos + 0;
                ibuf[ibufpos + 2] = vbufpos + 0;
                ibuf[ibufpos + 3] = vbufpos + 0;
                ibuf[ibufpos + 4] = vbufpos + 0;
                ibuf[ibufpos + 5] = vbufpos + 0;
            }
            else
            {
                RenderVertex v0 = default(RenderVertex);
                RenderVertex v1 = default(RenderVertex);
                RenderVertex v2 = default(RenderVertex);
                RenderVertex v3 = default(RenderVertex);
                v0.X = 0;
                v0.Y = 0;
                v0.Z = 0;
                v1.X = 0;
                v1.Y = 0;
                v1.Z = 0;
                v2.X = 0;
                v2.Y = 0;
                v2.Z = 0;
                v3.X = 0;
                v3.Y = 0;
                v3.Z = 0;


                vbuf[vbufpos + 0] = v0;
                vbuf[vbufpos + 1] = v1;
                vbuf[vbufpos + 2] = v2;
                vbuf[vbufpos + 3] = v3;
            }
        }
    }
    public class SpriteScale9
    {
        public Sprite sprite;
        public Border scaleborder;
    }



}
